package com.atguigu.gmall.realtime.app.dws;

import com.atguigu.gmall.realtime.app.BaseSqlApp;
import com.atguigu.gmall.realtime.bean.KeywordStats;
import com.atguigu.gmall.realtime.common.Constant;
import com.atguigu.gmall.realtime.function.IkAnalyzer;
import com.atguigu.gmall.realtime.util.FlinkSinkUtil;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneOffset;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/1 14:43
 */
public class DwsKeywordSearch extends BaseSqlApp {
    public static void main(String[] args) {
        new DwsKeywordSearch().init(4004, 1, "DwsKeywordSearch");
    }
    
    @Override
    protected void run(StreamTableEnvironment tenv) {
        tenv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(8));
        // 1. 创建动态表与source关联:kafka中的topic  dwd_page
        tenv.executeSql("create table page(" +
                            "   common map<string, string>, " +
                            "   page map<string, string>, " +
                            "   ts bigint, " +
                            "   et as to_timestamp(from_unixtime(ts/1000)), " +
                            "   watermark for et as et - interval '3' second" +
                            ")with(" +
                            "   'connector' = 'kafka', " +
                            "   'properties.bootstrap.servers' = 'hadoop162:9092,hadoop163:9092', " +
                            "   'properties.group.id' = 'DwsKeywordSearch', " +
                            "   'topic' = '" + Constant.TOPIC_DWD_PAGE + "', " +
                            "   'scan.startup.mode' = 'latest-offset', " +
                            "   'format' = 'json' " +
                            ")");
        
        // 1. 过滤出来需要的数据
        Table t1 = tenv.sqlQuery("select" +
                                     " page['item'] keyword, " +
                                     " et " +
                                     "from page " +
                                     "where page['page_id'] = 'good_list' and " +
                                     "page['item'] is not null and " +
                                     "page['item_type'] = 'keyword'");
        tenv.createTemporaryView("t1", t1);
        
        // 2. 对关键词进行分词
        // 2.1 注册函数
        tenv.createTemporaryFunction("ik_analyzer", IkAnalyzer.class);
        // 小米手机 华为手机
        // 2.2 进行列转行
        Table t2 = tenv.sqlQuery("select" +
                                     "  word, " +
                                     "  et " +
                                     "from t1 " +
                                     "join lateral table(ik_analyzer(keyword)) on true");
        tenv.createTemporaryView("t2", t2);
        // 3. 开窗聚合
        Table result = tenv.sqlQuery("select" +
                                         "  date_format(tumble_start(et, interval '5' second), 'yyyy-MM-dd HH:mm:ss') stt, " +
                                         "  date_format(tumble_end(et, interval '5' second), 'yyyy-MM-dd HH:mm:ss') edt," +
                                         "  word keyword, " +
                                         "  'search' source," +
                                         "  count(word) ct," +
                                         "  unix_timestamp() * 1000 ts " +
                                         "from t2 " +
                                         "group by " +
                                         " word," +
                                         " tumble(et, interval '5' second)");
        
        // 4. 数据写入到clickhouse中
        tenv
            .toAppendStream(result, KeywordStats.class)
            .addSink(FlinkSinkUtil
                         .getClickhouseSink(Constant.CLICKHOUSE_DB,
                                            Constant.CLICKHOUSE_TABLE_KEYWORD_STATS,
                                            KeywordStats.class));
        
    }
}
